package gov.fnal.mcginnis.threetone;

import java.awt.Color;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

import com.astrofizzbizz.pixie.PixieImage;
import com.astrofizzbizz.pixie.PixieImageRGBPlotter;



/**
 * @author mcginnis
 *
 */
public class ColorBox extends ImageIcon
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * @param ired
	 * @param igreen
	 * @param iblue
	 * @param ixsize
	 * @param iysize
	 * @param border 
	 */
	public ColorBox(int ired, int igreen, int iblue, int ixsize, int iysize, boolean border)
	{
		super(makeBufferedImage(ired, igreen, iblue, ixsize, iysize, border));
	}
	/**
	 * @param icolor
	 * @param ixsize
	 * @param iysize
	 * @param border 
	 */
	public ColorBox(int icolor, int ixsize, int iysize, boolean border)
	{
		super(makeBufferedImage((new Color(icolor)).getRed(), (new Color(icolor)).getGreen(), (new Color(icolor)).getBlue(), ixsize, iysize, border));
	}
	/**
	 * @param hexcolor
	 * @param ixsize
	 * @param iysize
	 * @param border 
	 */
	public ColorBox(String hexcolor, int ixsize, int iysize, boolean border)
	{
		super(makeBufferedImage(
				(new Color(Integer.valueOf(hexcolor, 16).intValue())).getRed(), 
				(new Color(Integer.valueOf(hexcolor, 16).intValue())).getGreen(), 
				(new Color(Integer.valueOf(hexcolor, 16).intValue())).getBlue(), ixsize, iysize, border));
	}
	private static BufferedImage makeBufferedImage(int ired, int igreen, int iblue, int ixsize, int iysize, boolean border)
	{
		PixieImage piRed = new PixieImage(ixsize,iysize);
		PixieImage piGreen = new PixieImage(ixsize,iysize);
		PixieImage piBlue = new PixieImage(ixsize,iysize);
		double[][]	redPixelArray = piRed.getPix();
		double[][]	greenPixelArray = piGreen.getPix();
		double[][]	bluePixelArray = piBlue.getPix();
		double dborderColor = 0.0;
		if ((ired < 10) && (igreen < 10) && (iblue < 10) ) dborderColor = 254.0;
		for (int ix = 0; ix < ixsize; ++ix)
		{
			for (int iy = 0; iy < iysize; ++iy)
			{
				redPixelArray[ix][iy] = (double) ired;
				greenPixelArray[ix][iy] = (double) igreen;
				bluePixelArray[ix][iy] = (double) iblue;
			}
		}
		if (border)
		{
			int ithick = 3;
			for (int ix = 0; ix < ixsize; ++ix)
			{
				for (int ij = 0; ij < ithick; ++ij)
				{
					redPixelArray[ix][ij] = dborderColor;
					greenPixelArray[ix][ij] = dborderColor;
					bluePixelArray[ix][ij] = dborderColor;
					redPixelArray[ix][iysize - 1 - ij] = dborderColor;
					greenPixelArray[ix][iysize - 1 - ij] = dborderColor;
					bluePixelArray[ix][iysize - 1 - ij] = dborderColor;
				}
			}
			for (int iy = 0; iy < iysize; ++iy)
			{
				for (int ij = 0; ij < ithick; ++ij)
				{
					redPixelArray[ij][iy] = dborderColor;
					greenPixelArray[ij][iy] = dborderColor;
					bluePixelArray[ij][iy] = dborderColor;
					redPixelArray[ixsize - 1 - ij][iy] = dborderColor;
					greenPixelArray[ixsize - 1 - ij][iy] = dborderColor;
					bluePixelArray[ixsize - 1 - ij][iy] = dborderColor;
				}
			}
		}
		PixieImageRGBPlotter pip = new PixieImageRGBPlotter();
		pip.setImages(piRed, piGreen, piBlue);
		pip.setAutoScale(false);
		pip.setColorSpectrum(false);
		pip.setInvertImage(false);
		pip.setPixelValueLimits(0.0, 255.0);
		pip.setScaleType("linear");
		return pip.makeBufferedImage();
	}
	/**
	 * @param inumColors
	 * @param colors
	 * @param mix
	 * @return int
	 */
	public static int addColors(int inumColors, int[] colors, double[] mix)
	{
		int[][] rgb = new int[inumColors][3];
		int[][] cym = new int[inumColors][3];
		double[] cymMix = new double[3];
		int[] rgbMix = new int[3];
		
		for (int ii = 0; ii < inumColors; ++ii)
		{
			rgb[ii][0] = (new Color(colors[ii])).getRed();
			rgb[ii][1] = (new Color(colors[ii])).getGreen();
			rgb[ii][2] = (new Color(colors[ii])).getBlue();
			cym[ii][0] = 255 - rgb[ii][0];
			cym[ii][1] = 255 - rgb[ii][1];
			cym[ii][2] = 255 - rgb[ii][2];
		}
		for (int ii = 0; ii < 3; ++ii)
		{
			cymMix[ii] = 0.0;
			for (int ij = 0; ij < inumColors; ++ij)
			{
				cymMix[ii] = cymMix[ii] + mix[ij] * ((double) cym[ij][ii] );
			}
			rgbMix[ii] = 255 - ((int) cymMix[ii]);
			if (rgbMix[ii] < 0) rgbMix[ii] = 0;
			if (rgbMix[ii] > 255) rgbMix[ii] = 255;
		}
		Color clr = new Color(rgbMix[0], rgbMix[1], rgbMix[2]);
		int irgb = clr.getRGB();
		return irgb;
	}
	/**
	 * @param icolor1
	 * @param icolor2
	 * @return double
	 */
	public static double compareColors(int icolor1, int icolor2)
	{
		Color clr1 = new Color(icolor1);
		Color clr2 = new Color(icolor2);
		int[] irgb1 = new int[3];
		int[] irgb2 = new int[3];
		irgb1[0] = clr1.getRed();
		irgb1[1] = clr1.getGreen();
		irgb1[2] = clr1.getBlue();
		irgb2[0] = clr2.getRed();
		irgb2[1] = clr2.getGreen();
		irgb2[2] = clr2.getBlue();
		
		double chi = 0.0;
		for (int ii = 0; ii < 3; ++ii)
		{
			double delta = ((double)(irgb1[ii] - irgb2[ii])) / 255.0;
			chi = chi + delta * delta  / 3.0;
		}
		return chi;
	}

}